
package common.packet;

import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;


/**
 * Interface for all packet types.
 *
 * @author xissburg
 */
public interface Packet
{
    /**
     * Reads one instance of this packet off in and sets this to be the packet
     * read. It doesn't read the packet code, it assumes the code was already
     * read and identified that this is the kind of packet received.
     * 
     * @param in
     */
    void read(DataInputStream in) throws IOException;

    /*
    void readWithCode(DataInputStream in) throws IOException
    {
        int code = in.readShort();

        if(code != getCode())
            throw new IllegalArgumentException("");

        read(in);
    }*/

    /**
     * Writes the contents of this packet to out. It can later be read from the
     * corresponding input stream using read().
     * @param out
     */
    void write(DataOutputStream out) throws IOException;

    /**
     * Returns the packet code, a short (2 bytes) number.
     *
     * @return This' packet code.
     */
    Code getCode();

    /**
     * Packets must be copyable. This method must return a deep copy of this packet.
     *
     * @return A deep copy of this packet
     */
    Packet copy();

    
    /**
     * Packet codes.
     */
    enum Code
    {
        AUTHENTICATION,

        AUTHORIZATION,

        CHAT_MESSAGE,

        CLIENT_IN,

        CLIENT_OUT,

        CLIENT_INFO,

        INPUT,

        ERROR,

        GAME_STATE,

        WORLD_STATE,

        LOGOUT,

        OK,

        START_GAME,

        STOP_GAME,

        ARE_YOU_ALIVE,

        I_AM_ALIVE,

        PING,

        PONG,

        BODY_STATE
    }
}
